import Vue from 'vue';
import Vuex from 'vuex';
import * as types from './mutation-types';

Vue.use(Vuex);

import home from './modules/home';

const createStore = () => {
	return new Vuex.Store({
		state: {
			currentModel: 'home',
			showTabbar: true,
			swipeDirection: '',
			swipeClass: {
				header: '',
				btn_circle: '',
				tabbar: ''
			},
			hasNetwork: true,
			cookies: {}
		},
		actions: {
			resetSwipeClass ({ commit }) {
				commit(types.resetSwipe);
			},
			swipe ({ commit, state }, { direction, distance }) {
				if (state.swipeDirection === direction) {
					return;
				}

				if (direction === 'down') {
					commit(types.swipeDown);
				}
				else if (direction === 'up') {
					commit(types.swipeUp);
				}
			},
			updateCurrentModel ({ commit }, model) {
				commit(types.updateCurrentModel, model);
			},
			// 网络环境判断
			changeNetworkStatus ({ commit }, status) {
				commit(types.changeNetworkStatus, status);
			}
		},
		mutations: {
			[types.resetSwipe] (state) {
				state.swipeDirection = '';
				state.swipeClass = {
					header: '',
					btn_circle: '',
					tabbar: ''
				};
			},
			[types.swipeDown] (state) {
				state.swipeDirection = 'down';
				state.swipeClass.header = 'header-up';
				state.swipeClass.tabbar = 'navigator-down';
				state.swipeClass.btn_circle = 'btn-circle-down';
			},
			[types.swipeUp] (state) {
				state.swipeDirection = 'up';
				state.swipeClass.header = 'header-down';
				state.swipeClass.tabbar = 'navigator-up';
				state.swipeClass.btn_circle = 'btn-circle-up';
			},
			[types.updateCurrentModel] (state, model) {
				if (['home', 'find', 'bell', 'comment', 'navicon'].indexOf(model) < 0) {
					state.showTabbar = false;
				}
				else {
					state.currentModel = model;
					state.showTabbar = true;
				}
			},
			[types.changeNetworkStatus] (state, status) {
				state.hasNetwork = status;
			}
		},
		modules: {
			home
		}
	});
};

export default createStore;
